﻿package data.processor;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Fang Lu, fanglupku@gmail.com
 */

public class BasePageDealer {

	public static String eraseEmptyLine(String originString) {
		StringBuffer sb = new StringBuffer();
		String sa[] = originString.split("\n");
		for (int i = 0; i < sa.length; ++i) {
			String temp = sa[i].trim();
			if (temp.length() > 0) {
				sb.append(sa[i]);
				sb.append("\n");
			}
		}
		return sb.toString();
	}

	public static String eraseSpareSpace(String originString) {
		String str_pattern = "[\\s]+";
		Pattern pattern = Pattern.compile(str_pattern, Pattern.DOTALL
				| Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(originString);
		String result = matcher.replaceAll(" ");
		return result;
	}

	public static String eraseOneTag(String originString, String tag) {
		String str_pattern = "<(/{0,1})(\\s*?)(" + tag
				+ ")((\\s*?)|(\\s+?[^>]*?))(/{0,1})>";
		Pattern pattern = Pattern.compile(str_pattern, Pattern.DOTALL
				| Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(originString);
		String result = matcher.replaceAll("");
		return result;
	}

	public static String replaceOneTag(String originString, String oldTag,
			String newTag) {
		String str_pattern = "<(\\s*?)(" + oldTag
				+ ")((\\s*?)|(\\s+?[^>]*?))/>";
		Pattern pattern = Pattern
				.compile(str_pattern, Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(originString);
		String result = matcher.replaceAll("<" + newTag + "/>");
		str_pattern = "</(\\s*?)(" + oldTag + ")((\\s*?)|(\\s+?[^>]*?[^/]))>";
		pattern = Pattern.compile(str_pattern, Pattern.CASE_INSENSITIVE);
		matcher = pattern.matcher(result);
		result = matcher.replaceAll("</" + newTag + ">");
		str_pattern = "<(\\s*?)(" + oldTag + ")((\\s*?)|(\\s+?[^>]*?[^/]))>";
		pattern = Pattern.compile(str_pattern, Pattern.CASE_INSENSITIVE);
		matcher = pattern.matcher(result);
		result = matcher.replaceAll("<" + newTag + ">");
		return result;
	}

	public static String eraseOneTagAndContent(String originString, String tag) {
		String str_pattern = "<(\\s*?)(" + tag
				+ ")((\\s*?)|(\\s+?[^>]*?[^/]))>" + ".*?" + "</(\\s*?)(" + tag
				+ ")((\\s*?)|(\\s+?[^>]*?))>";
		Pattern pattern = Pattern.compile(str_pattern, Pattern.DOTALL
				| Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(originString);
		String result = matcher.replaceAll("");
		str_pattern = "<(/{0,1})(\\s*?)(" + tag.toString()
				+ ")((\\s*?)|(\\s+?[^>]*?))(/{0,1})>";
		pattern = Pattern.compile(str_pattern, Pattern.DOTALL
				| Pattern.CASE_INSENSITIVE);
		matcher = pattern.matcher(result);
		result = matcher.replaceAll("");
		return result;
	}

	public static ArrayList<String> extractOneTagContent(String originString,
			String tag) {
		ArrayList<String> resultList = new ArrayList<String>();
		String str_pattern = "<(\\s*?)(" + tag
				+ ")((\\s*?)|(\\s+?[^>]*?[^/]))>(.*?)</\\s*?" + tag + "\\s*?>";
		Pattern pattern = Pattern.compile(str_pattern, Pattern.DOTALL
				| Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(originString);
		while (matcher.find()) {
			String temp = new String(originString.substring(matcher.start(),
					matcher.end()));
			resultList.add(temp);
		}
		str_pattern = "<(\\s*?)(" + tag + ")((\\s*?)|(\\s+?[^>]*?))/>";
		pattern = Pattern.compile(str_pattern, Pattern.DOTALL
				| Pattern.CASE_INSENSITIVE);
		matcher = pattern.matcher(originString);
		while (matcher.find()) {
			String temp = new String(originString.substring(matcher.start(),
					matcher.end()));
			resultList.add(temp);
		}
		return resultList;
	}

	public static ArrayList<String> extractOneTagContent(String originString,
			String tag, String AttributeName, String AttributeValue) {
		ArrayList<String> resultList = new ArrayList<String>();
		String str_pattern = "<(\\s*?)(" + tag + ")((\\s*?)|(\\s+?[^>]*?))("
				+ AttributeName + "=." + AttributeValue
				+ ".)((\\s*?)|(\\s+?[^>]*?[^/]))>(.*?)</\\s*?" + tag + "\\s*?>";
		Pattern pattern = Pattern.compile(str_pattern, Pattern.DOTALL
				| Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(originString);
		while (matcher.find()) {
			String temp = new String(originString.substring(matcher.start(),
					matcher.end()));
			resultList.add(temp);
		}
		str_pattern = "<(\\s*?)(" + tag + ")((\\s*?)|(\\s+?[^>]*?))("
				+ AttributeName + "=.{0,1}" + AttributeValue
				+ ".)((\\s*?)|(\\s+?[^>]*?))/>";
		pattern = Pattern.compile(str_pattern, Pattern.DOTALL
				| Pattern.CASE_INSENSITIVE);
		matcher = pattern.matcher(originString);
		while (matcher.find()) {
			String temp = new String(originString.substring(matcher.start(),
					matcher.end()));
			resultList.add(temp);
		}
		return resultList;
	}

	public static String eraseAllTags(String content) {
		if (null != content) {
			String str_pattern = "<.*?>";
			Pattern pattern = Pattern.compile(str_pattern, Pattern.DOTALL
					| Pattern.CASE_INSENSITIVE);
			Matcher matcher = pattern.matcher(content);
			String result = matcher.replaceAll("");
			return result;
		} else {
			return null;
		}
	}

	public static int getAllTags(String content, HashSet<String> tagSet) {
		String str_pattern = "<.*?>";
		Pattern pattern = Pattern.compile(str_pattern, Pattern.DOTALL
				| Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(content);
		while (matcher.find() && matcher.start() < matcher.end() - 2) {
			String temp = new String(content.substring(matcher.start() + 1,
					matcher.end() - 1));
			if (temp.startsWith("/") && temp.length() > 1) {
				temp = new String(temp.substring(1));
			}
			temp = temp.trim();
			if (temp.indexOf(" ") > 0) {
				temp = new String(temp.substring(0, temp.indexOf(" ")));
			}
			if (temp.indexOf("\t") > 0) {
				temp = new String(temp.substring(0, temp.indexOf("\t")));
			}
			if (!tagSet.contains(temp)) {
				tagSet.add(temp);
			}
		}
		return tagSet.size();
	}

	public static int parserKeywordsByURL(String urlString,
			ArrayList<String> keywordList) {
		StringTokenizer st = new StringTokenizer(urlString, "/?.");
		while (st.hasMoreTokens()) {
			String thisToken = st.nextToken();
			if (thisToken.equalsIgnoreCase("http:")
					|| thisToken.equalsIgnoreCase("https:")) {

			} else {
				keywordList.add(thisToken);
			}
		}
		return 0;
	}

	public static String eraseAllComment(String originString) {
		String str_pattern = "<!--(.*?)-->";
		Pattern pattern = Pattern.compile(str_pattern, Pattern.DOTALL
				| Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(originString);
		// while(matcher.find()){
		// System.out.println("====" + originString.substring(matcher.start(),
		// matcher.end()));
		// }
		String result = matcher.replaceAll("");
		return result;
	}

	public static String extractAttribute(String originString,
			String attributeName) {
		String result = "";
		int start = originString.indexOf(attributeName + "=");
		if (start > 0) {
			start += 8;
		} else {
			return result;
		}
		char endChar = '\"';
		int end = start + 1;
		if (originString.charAt(start) == '"'
				|| originString.charAt(start) == '\'') {
			endChar = originString.charAt(start);
			++start;
			end = originString.indexOf(endChar, start);
			if (end < 0) {
				end = start;
			}
			result = new String(originString.substring(start, end));
		} else {
			endChar = ' ';
			end = originString.indexOf(endChar, start);
			if (end < 0) {
				endChar = '>';
				end = originString.indexOf(endChar, start) - 1;
				if (end < 0) {
					end = start;
				}
			}
			result = new String(originString.substring(start, end));
		}
		return result;
	}
}
